package net.stogi.unicore.beyond.ui.client.taskeditor.editormenu;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.unicore.beyond.api.model.TaskDefinition;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.SortDir;
import com.extjs.gxt.ui.client.data.BaseModel;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

class TaskSelectorWidget extends Window implements TaskSelectorView {

	private class TaskModel extends BaseModel {

		private static final long serialVersionUID = 5994190319072359534L;

		public TaskModel(String name, String version) {
			set("name", name);
			set("version", version);
		}

		public String getName() {
			return get("name");
		}
	}
	
	private final Button cancelButton = new Button();

	private final Button openButton = new Button();

	private final Grid<TaskModel> taskGrid;

	private final ListStore<TaskModel> taskStore = new ListStore<TaskModel>();
	
	public TaskSelectorWidget() {
		
		List<ColumnConfig> columnConfigList = new ArrayList<ColumnConfig>();

		columnConfigList.add(new ColumnConfig("name", "Name", 150));
		columnConfigList.add(new ColumnConfig("version", "Version", 150));

		for (ColumnConfig columnConfig : columnConfigList) {
			columnConfig.setMenuDisabled(true);
		}
		
		taskGrid = new Grid<TaskModel>(taskStore, new ColumnModel(columnConfigList));
		
		initComposite();
		initI18n();
		initLayout();
	}

	@Override
	public void appear() {
		show();
	}

	@Override
	public void clear() {
		taskStore.removeAll();
	}

	@Override
	public void disapper() {
		hide();
	}

	@Override
	public void displayTaskDefinitions(List<TaskDefinition> taskDefinitions) {
		taskStore.removeAll();
		List<TaskModel> taskDefinitionsModel = new ArrayList<TaskModel>();
		for (TaskDefinition taskDefinition : taskDefinitions) {
			taskDefinitionsModel.add(new TaskModel(taskDefinition.getApplicationName(), taskDefinition.getApplicationVersion()));
		}
		taskStore.add(taskDefinitionsModel);
		taskStore.setSortField("name");
		taskStore.setSortDir(SortDir.ASC);
		taskStore.sort("name", SortDir.ASC);
	}

	@Override
	public void setCancelBehavior(final Behavior behavior) {
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setOpenBehavior(final Behavior behavior) {
		openButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setTaskDoubleClickedBehavior(final BehaviorWithValue<String> behavior) {
		taskGrid.addListener(Events.CellDoubleClick, new Listener<GridEvent<TaskModel>>() {

			@Override
			public void handleEvent(GridEvent<TaskModel> be) {
				TaskModel selectedItem = be.getGrid().getSelectionModel().getSelectedItem();
				if (selectedItem != null) {
					behavior.behave(selectedItem.getName());
				}
			}
		});
	}

	private void initComposite() {
		taskGrid.setAutoExpandColumn("name");
		taskGrid.setBorders(true);
		taskGrid.setEnableColumnResize(false);
		taskGrid.setHeight(250);
		taskGrid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		taskStore.setSortField("name");
		taskStore.setSortDir(SortDir.ASC);
		
		setBodyBorder(false);
		setClosable(false);
		setFrame(true);
		setLayout(new FitLayout());
		setModal(true);
		setResizable(false);
		setWidth("30%");
	}

	private void initI18n() {
		cancelButton.setText("Cancel");
		openButton.setText("Open");
		setHeading("Task Definition Selector");
	}

	private void initLayout() {
		add(taskGrid);
		addButton(openButton);
		addButton(cancelButton);
	}

	@Override
	public void setTaskSelectedBehavior(final BehaviorWithValue<String> behavior) {
		taskGrid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<TaskModel>() {
			
			@Override
			public void selectionChanged(SelectionChangedEvent<TaskModel> se) {
				TaskModel selectedItem = se.getSelectedItem();
				if (selectedItem != null) {
					behavior.behave(selectedItem.getName());
				}
			}
		});
	}
}
